@namespace SwashbucklerDiary.Rcl.Components
@inherits DialogComponentBase

<CustomMDialog MyValue="Visible"
               MyValueChanged="InternalVisibleChanged"
               ContentClass="rounded-lg"
               MaxWidth="420"
               Scrollable="true"
               OnBeforeShowContent="BeforeShowContent">
    <MCard>
        <MCardTitle Class="font-weight-bold flex-nowrap">
            <wc-auto-scroll-text>@(I18n.T("Select location"))</wc-auto-scroll-text>
            <MSpacer></MSpacer>
            <MButton Icon
                     OnClick="() => showSearch = !showSearch">
                <MIcon>search</MIcon>
            </MButton>
            <MButton Icon
                     OnClick="HandleOnCancel">
                <MIcon>close</MIcon>
            </MButton>
        </MCardTitle>

        <MCardText Class="pb-0 px-0">
            @if (showSearch)
            {
                <div class="mb-2 px-6">
                    <CustomMTextField @bind-Value="_searchText"
                                      Class="mb-2"
                                      OnInput="UpdateInternalItems"
                                      Dense
                                      TValue="string"
                                      Filled
                                      Rounded
                                      Clearable
                                      Color="currentColor"
                                      HideDetails="@("auto")"
                                      spellcheck="false"
                                      Placeholder="@(I18n.T("Search"))"
                                      PrependInnerIcon="search">
                    </CustomMTextField>
                </div>
            }
            <MList Class="py-0 scroll-show" Style="max-height:300px;">
                <Virtualize Items="internalItems"
                            ItemSize="56"
                            Context="item">
                    <MListItem @key="item.Id"
                               Class="px-10"
                               OnClick="() => SetSelectedLocation(item)">
                        <MListItemContent>
                            <wc-auto-scroll-text>@(item.Name)</wc-auto-scroll-text>
                        </MListItemContent>

                        <MListItemAction>
                            @if (item.Name == selectedLocation)
                            {
                                <MIcon>
                                    check
                                </MIcon>
                            }
                        </MListItemAction>
                    </MListItem>
                </Virtualize>
            </MList>
        </MCardText>
        <MCardActions>
            <MButton Text="true"
                     OnClick="() => showAdd = true">
                <wc-auto-scroll-text style="max-width:100px;">@(I18n.T("Add"))</wc-auto-scroll-text>
            </MButton>
            <MSpacer></MSpacer>
            <MButton Text="true"
                     OnClick="HandleOnCancel">
                @(I18n.T("Cancel"))
            </MButton>
            <MButton Text="true"
                     Color="primary"
                     OnClick="HandleOnOK">
                @(I18n.T("OK"))
            </MButton>
        </MCardActions>
    </MCard>
</CustomMDialog>

<InputDialog @bind-Visible="showAdd"
             Title="@(I18n.T("Add location"))"
             Placeholder="@(I18n.T("Please enter a location name"))"
             MaxLength="200"
             OnOK="SaveAdd">
</InputDialog>